package org.example.web.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Component
@Aspect
public class TranslateAspect {
    Logger logger = LoggerFactory.getLogger(TranslateAspect.class);
    @Autowired
    TemplateEngine templateEngine;

    @Pointcut("execution(* org.example.web.controller.*.*(..))")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object translate(ProceedingJoinPoint jp) throws Throwable {
        Object data = jp.proceed(jp.getArgs());
        if (data != null) {
            try {
                Context context = new Context();
                context.setVariable("obj", data);
                String result = templateEngine.process("user", context);
                return result;
            } catch (Exception ex) {
                logger.error("template translate failed", ex);
            }
        }
        return null;
    }
}
